const CompressionPlugin = require('compression-webpack-plugin');
const webpack = require('webpack');
const WebpackBar = require('webpackbar');
const path = require('path')
const resolve = (dir) => path.join(__dirname, dir)
module.exports = {
  publicPath: '/',
  runtimeCompiler: true,
  configureWebpack: config => {
    config.plugins = [
      ...config.plugins,
      new WebpackBar({
        name: 'eip-manage',
      }),
      new webpack.IgnorePlugin({
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/
      })
    ]
    if (process.env.NODE_ENV === 'production') {
      // gzip压缩
      config.plugins = [
        ...config.plugins,
        new CompressionPlugin({
          // 要压缩的文件类型
          test: /\.js$|\.html$|\.css/,
          // 大于该尺寸的文件才压缩（单位byte)
          threshold: 10240,
          // 是否删除原文件
          deleteOriginalAssets: false
        }),
      ];
    }
    const resultConfig = {
      resolve: {
        alias: {
          '@': resolve('src'),
        },
      },
      plugins: [
        new webpack.IgnorePlugin({
          resourceRegExp: /^\.\/locale$/,
          contextRegExp: /moment$/,
        }),
        new WebpackBar({
            name: 'EIP-MANAGE',
        }),
      ],
    }
    if (process.env.NODE_ENV !== 'development') {
      resultConfig.plugins.push(
          new CompressionPlugin({
            // 要压缩的文件类型
            test: /\.js$|\.html$|\.css/,
            // 大于该尺寸的文件才压缩（单位byte)
            threshold: 10240,
            // 是否删除原文件
            deleteOriginalAssets: false,
          })
      )
    }
    return resultConfig
  },
  chainWebpack: config => {
    config.plugins.delete('preload')
    config.plugins.delete('prefetch')
    config.devServer.disableHostCheck(true);
    config.entry('index').add('babel-polyfill');
    if (process.env.NODE_ENV === 'production') {
      config.performance.set('hints', false)
      config.devtool('none')
      // 配置打包时分包的数量上限和单个文件的最小尺寸
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          elementUI: {
            name: 'chunk-elementUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
          },
          hotentUI: {
            name: 'chunk-hotentUI',
            priority: 20,
            test: /[\\/]node_modules[\\/]_?hotent-ui(.*)/,
          },
        },
      })
    }

  },
  // 不生成sourceMap
  productionSourceMap: false,
  css: {
    loaderOptions: {
      sass: {
        prependData: `@import "~@/assets/css/element-variables.scss";`
      },
    },
  },
}
